package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyRepository strategyRepository;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;


    @Override
    public Strategy get(String id) {
        return strategyRepository.findById(id).get();
    }

    @Override
    public List<Strategy> selectAll() {
        return strategyRepository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //创建Query对象,根据过滤条件查询总条数,此处没有限制条件,所以用空的查询对象查
        Query query = new Query();
        //添加限制,如果有目的地
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }

        //创建过滤查询
        //有数据,进行分页操作,创建新的分页对象,此索引从0开始,所以要减一,分页的字段与数据库一致
        Pageable pageAble = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        //返回给控制器
        return DBHelper.query(template, Strategy.class, query, pageAble);
    }

    @Override
    public void update(Strategy strategy) {
        //查询并给攻略添加目的地名,目的地Id,顺便添加是否是国外
        destinationService.addDest(strategy);
        //查询并给攻略添加分类名
        strategyCatalogService.addCatalog(strategy);
        //查询并给攻略添加是主题名
        strategyThemeService.addThemeName(strategy);
        //查询并给攻略添加点赞数,创建时间,等不改变的字段
        Strategy strategy1 = this.get(strategy.getId());
        strategy.setFavornum(strategy1.getFavornum());
        strategy.setCreateTime(strategy1.getCreateTime());
        strategy.setReplynum(strategy1.getReplynum());
        strategy.setSharenum(strategy1.getSharenum());
        strategy.setThumbsupnum(strategy1.getThumbsupnum());
        strategy.setViewnum(strategy1.getViewnum());
        strategyRepository.save(strategy);
    }

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        //除了前端传来的   外,还需要destId    createtime   isabroad
        // 点赞数可以默认     themeName主题名    分类名    目的地名
        //title: 吃
        //subTitle: 吃
        //coverUrl: https://liu12424.oss-cn-shenzhen.aliyuncs.com/d7627245-d633-4bee-8d72-0066626fff1e.png
        //catalogId: 5e3002138c1b00008c000712
        //themeId: 5e3286e8304b00003b0053da
        //state: 1
        //summary: 吃吃
        //content: <p>4515156515</p>
        //查询并给攻略添加目的地名,目的地Id,顺便添加是否是国外
        destinationService.addDest(strategy);
        //查询并给攻略添加分类名
        strategyCatalogService.addCatalog(strategy);
        //查询并给攻略添加是主题名
        strategyThemeService.addThemeName(strategy);
        //添加创建时间如果是更新则不需要添加创建时间,进行更新操作
        strategy.setCreateTime(new Date());
        strategyRepository.save(strategy);
    }

    @Override
    public void deleteById(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public void changeState(String id, int state) {
        //利用Update对象,实现只更新部分字段
        //Query对象用于限制更新哪些行
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        //Update对象,用于指明只更新哪些字段,并对应值     相当于set
        Update update = new Update();
        update.set("state", state);
        //最后利用MongoTemplate对象,执行操作
        template.updateMulti(query, update, Strategy.class);
    }

    @Override
    public List<Strategy> selectViewnumTop3(String destId) {
        //利用template+Query+PageAble完成
        //不需要任何条件,空的Query即可
        Query query = new Query();
        query.addCriteria(Criteria.where("destId").is(destId));
        //按照热度排序,要前三条数据
        Pageable pageable=PageRequest.of(0, 3, Sort.Direction.DESC,"viewnum");
        //执行
        query.with(pageable);
        List<Strategy> strategies = template.find(query, Strategy.class);

        return strategies;
    }

    @Override
    public List<Strategy> selectByDestId(String destId) {
        List<Strategy> list = strategyRepository.findByDestIdOrderByViewnumDesc(destId);
        return list;
    }

    @Override
    public void saveVo(StrategyStatisVO vo) {
        Strategy strategy = this.get(vo.getStrategyId());
        BeanUtils.copyProperties(vo, strategy);
        //保存
        strategyRepository.save(strategy);
    }

    @Override
    public Strategy getForFirst() {
        Query query = new Query();
        Pageable pageable=PageRequest.of(0, 1, Sort.Direction.DESC,"favornum");
        query.with(pageable);
        List<Strategy> list = template.find(query, Strategy.class);
        return list.get(0);
    }
}
